package com.chf.items;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.PickaxeItem;
import net.minecraft.world.item.Tiers;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

/**
 * 下界合金镐
 *
 * @author niooooo
 */
public class ObsidianPickaxe extends PickaxeItem {

    /**
     * 创建一个镐子
     *
     * @param attackDamage 武器伤害 --> 最终伤害 = 基础伤害 + 物品材质伤害 + 武器伤害。基础伤害为1.0
     * @param attackSpeed  攻击速度 --> 最终攻速 = 基础攻速 + 武器攻速。基础攻速为4.0
     * @param properties   属性 --> 物品的最大堆叠栈(数量),物品的最大耐久值,物品的所在创造物品栏位置等等
     */
    public ObsidianPickaxe(int attackDamage, float attackSpeed, Properties properties) {
        super(Tiers.NETHERITE, attackDamage, attackSpeed, properties);
    }

    /**
     * 右键点击方块时触发（放置蜡烛）
     *
     * @param useOnContext {@link UseOnContext} 上下文
     * @return {@link InteractionResult}
     */
    @Override
    public @NotNull InteractionResult useOn(@NotNull UseOnContext useOnContext) {
        // 获取被右键的方块所在位置、方向
        Level level = useOnContext.getLevel();
        BlockPos blockPos = useOnContext.getClickedPos();
        Direction direction = useOnContext.getClickedFace();

        // 只在服务器端执行
        //if (!level.isClientSide()) {
        // TODO 测试
        if (true) {
            // 1、如果右键的是方块的上面，就用默认的蜡烛；否则就用贴墙的蜡烛
            BlockState state;
            if (Direction.UP.equals(direction)) {
                state = Blocks.TORCH.getStateForPlacement(new BlockPlaceContext(useOnContext));
            } else {
                state = Blocks.WALL_TORCH.getStateForPlacement(new BlockPlaceContext(useOnContext));
            }
            // 2、放置蜡烛并更新（参数3 表示强制更新并通知客户端）
            if (state != null) {
                level.setBlock(blockPos.relative(direction), state, 3);
            }
            return InteractionResult.CONSUME;
        }
        return InteractionResult.SUCCESS;
    }
}
